package com.thinkgem.jeesite.modules.website;

import com.thinkgem.jeesite.modules.guide.entity.Guiding;
import com.thinkgem.jeesite.modules.guide.service.GuidingService;
import com.thinkgem.jeesite.modules.intelligent.entity.IntelligentController;
import com.thinkgem.jeesite.modules.intelligent.service.IntelligentControllerService;
import com.thinkgem.jeesite.modules.lane.entity.Lane;
import com.thinkgem.jeesite.modules.signallight.entity.TSignalLight;
import com.thinkgem.jeesite.modules.signallight.service.TSignalLightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xlp
 * @date 2019/5/16
 */
@RestController
@RequestMapping("/intelligent")
public class IntelligentInfoController {

    @Autowired
    private IntelligentControllerService intelService;

    @Autowired
    private GuidingService guidingService;

    @Autowired
    private TSignalLightService signalLightService;

    /**
     * 获取信号灯和引导灯
     * @param request
     * @return
     */
    @RequestMapping("/getGuidingAndLane")
    public List<Guiding> getGuidingAndLane(HttpServletRequest request){
        List<Guiding> guidingList = guidingService.findList(new Guiding());
        List<TSignalLight> signalLightList = signalLightService.findList(new TSignalLight());
        if (!CollectionUtils.isEmpty(guidingList)){
            if (!CollectionUtils.isEmpty(signalLightList)){
                for (TSignalLight signalLight:signalLightList){
                    Guiding guiding = new Guiding();
                    guiding.setId(signalLight.getId());
                    guiding.setGuidingName(signalLight.getLightname());
                    guidingList.add(guiding);
                }
            }
        }
        return guidingList;
    }

    /**
     * 新增或修改智能控制信息
     * @param request
     * @param intelligent
     */
    @RequestMapping(value = "/addOrUpdateIntelligent", method = RequestMethod.POST)
    public String addOrUpdateIntelligent(HttpServletRequest request, IntelligentController intelligent) {
        intelService.save(intelligent);
        return "";
    }

    /**
     * 获取单个智能控制信息
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/getIntelligent", method = RequestMethod.POST)
    public IntelligentController getIntelligent(HttpServletRequest request, String id) {
        IntelligentController intelligent = intelService.get(id);
        return intelligent;
    }

    /**
     * 获取智能控制信息集合
     * @param request
     * @return
     */
    @RequestMapping(value = "/getIntelligentList", method = RequestMethod.POST)
    public ResponseData getIntelligentList(HttpServletRequest request) {
        List<IntelligentController> intelligentList = intelService.findList(new IntelligentController());
        ResponseData responseData = new ResponseData();
        if (!CollectionUtils.isEmpty(intelligentList)){
            for (IntelligentController intelligent:intelligentList){
                //引导灯
                TSignalLight signalLight = signalLightService.get(intelligent.getLightId());
                if (signalLight==null){
                    Guiding guiding = guidingService.get(intelligent.getLightId());
                    if (guiding!=null){
                        intelligent.setLightName(guiding.getGuidingName());
                    }
                }else {
                    intelligent.setLightName(signalLight.getLightname());
                }

                //控制方式
                if ("B".equals(intelligent.getControlStyle())){
                    intelligent.setControlStyle("手动");
                    //绿灯或红灯
                    if ("0".equals(intelligent.getStatus())){
                        intelligent.setStatus("红灯");
                    }
                    if ("1".equals(intelligent.getStatus())){
                        intelligent.setStatus("绿灯");
                    }
                }
                if ("A".equals(intelligent.getControlStyle())){
                    intelligent.setControlStyle("自动");
                    intelligent.setStatus("");
                }
            }
            responseData.setCode(200);
            responseData.setMsg("查询成功");
            responseData.setTotal(String.valueOf(intelligentList.size()));
            responseData.setData(intelligentList);
        }else {
            responseData.setCode(200);
            responseData.setMsg("查询失败");
            responseData.setTotal("0");
            responseData.setData(null);
        }
        return responseData;
    }

    /**
     * 删除/批量删除智能控制信息
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(HttpServletRequest request, String id) {
        String res = "";
        String[] ids = id.split(",");
        IntelligentController intelligent = new IntelligentController();
        try {
            for (int i=0; i<ids.length; i++){
                intelligent.setId(ids[i]);
                intelService.delete(intelligent);
            }
            res = "200";
        }catch (Exception e){
            e.printStackTrace();
            res = "201";
        }
        return res;
    }
}
